﻿using IBLL.SH_ADF0979IBLL;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using MysqlforDataWatch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RLDA_VehicleData_Watch.Controllers.DataProcess
{
    public class GPSTrackController : Controller
    {
        private readonly DbContext _db;
        private readonly ILogger<AnalysisController> _logger;
        private readonly IMemoryCache _memoryCache;//内存缓存
        private readonly IGPSRecord_IBLL _IGPSRecord_Service;
        public GPSTrackController(IGPSRecord_IBLL IGPSRecord_Service, ILogger<AnalysisController> logger, IMemoryCache memoryCache, DbContext db)
        {
            _logger = logger;   
            _memoryCache = memoryCache;
            _db = db;
            _IGPSRecord_Service=IGPSRecord_Service; 
        }
        /// <summary>
        /// 获得所有车辆名称
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetGPSVehicles()
        {
            List<string> vehicles = new List<string>();
            await Task.Run(() => {
                vehicles = _db.Set<t_vehicleimportpara>().Select(a => a.vehicleid).ToList();
            });
            return Json(vehicles);
        }

        public async Task<IActionResult> GetTrack(string startdate, string enddate, string vehicleid,int gpspoints)
        {
            var GPScash = await _memoryCache.GetOrCreateAsync<JsonResult>("GetTrack" + startdate + enddate + vehicleid, async value =>
            {

                var sd = Convert.ToDateTime(startdate);
                var ed = Convert.ToDateTime(enddate);
                //这里的reducetimeforgps是appsetting里的

                var GPSList = await _IGPSRecord_Service.LoadGPSRecord(sd, ed, vehicleid, gpspoints);
                //var lat = GPSList.Select(a => a.Lat);
                //var lon= GPSList.Select(a => a.Lon);
                var json = Json(GPSList);//这里speedlist是iquerable匿名类型，不知为什么无法传给前端layui表格，所以只能直接传json数据
                return json;
            });
            if (GPScash != null)
            {
                return GPScash;
            }
            else
            {
                return Json("没有轨迹数据！");
            }

        }


    }
}
